{% extends "back.html" %}
{% block hello %}
    {% import "fenye.html" as macros %}
    <div class="tpl-content-wrapper" style="margin-top: -7px;">
        <div class="tpl-content-page-title"></div>
        <div class="tpl-portlet-components">
            <div class="portlet-title">
                <div class="caption font-green bold">
                    <span class="am-icon-code"></span> 管理用户
                </div>
            </div>
            {% with messages = get_flashed_messages() %}
                {% if messages %}
                    <ul class='flashes'>
                        {% for message in messages %}
                            <li style="color:red">{{ message }}</li>
                        {% endfor %}
                    </ul>
                {% endif %}
            {% endwith %}
            <div style="margin-top: 10px">
                <a onclick="adduser()">
                    <input type="button" name="" class="am-btn am-btn-default am-btn-success" value="添加">
                </a>
            </div>
            <table class="am-table am-table-striped am-table-hover table-main">
                <caption>
                    <strong>用户列表</strong>
                </caption>
                <thead>
                <tr>
                    <th>用户id</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>职位</th>
                    <th>所属项目</th>
                    <th>状态</th>
                    <th>管理</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for user in users %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>
                            <label class="label label-default">{{ user.username }}</label>
                        </td>
                        <td>
                            <label class="label label-primary">{{ user.user_email }}</label>
                        </td>
                        <td>
                            <label class="label label-info">{{ user.works }}</label>
                        </td>
                        <td>
                            {% for i in user.quanxians %}
                                {% if i.rose==1 %}
                                    <label class="label label-default {{ user.username }}" id="{{ i.projects.id }}">
                                        {{ i.projects.project_name }}</label>
                                {% elif i.rose==2 %}
                                    <label class="label label-danger  {{ user.username }}" id="{{ i.projects.id }}">
                                        {{ i.projects.project_name }}</label>
                                {% endif %}
                            {% endfor %}
                        </td>
                        {% if user.status==0 %}
                            <td>
                                <a href="{{ url_for('user.fre_ad',id=user.id) }}">
                                    <input type="button" value="冻结" class="btn btn-success">
                                </a>
                            </td>
                        {% elif user.status==1 %}
                            <td>
                                <a href="{{ url_for('user.fre_re',id=user.id) }}">
                                    <input type="button" value="解冻" class="btn btn-danger">
                                </a>
                            </td>
                        {% endif %}
                        <td>
                            <a>
                                <input type="button" value="设置" class="btn btn-default"
                                       onclick="shezhiguanliyuan(id=this.id)" id="{{ user.username }}">
                            </a>
                        <td>
                        <span>
                            <a href="{{ url_for('user.red_pass',id=user.id) }}">
                                <input type="button" value="重置密码" class="btn btn-warning">
                            </a>
                        </span>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            <nav aria-label="Page navigation" class="pull-right">
                <ul class="pagination">
                    {{ html|safe }}
                </ul>
            </nav>
        </div>
    </div>
    <div style="display:none;position:fixed;z-index: 210;width: 800px;top: 100px;left: 20%;box-shadow: 0 0 10px rgba(0,0,0,0.6);border: solid 1px #255c71;border-radius: 5px 5px 0 0;background-color: #9eacb4"
         id="ceshiyongli">
        <div style="height: 5px;">请选择设置管理的项目：</div>
        <div style="background-color: whitesmoke;margin-top: 24px">
            <span style="text-align: center">
                <strong>项目：</strong>
            </span>
            <select id="xuanzehuanjing">
            </select>
        </div>
        <div style="margin-top: 50%;margin-bottom: auto">
            <input value="设置" type="button" class="btn-default shezhiguanliyuan" onclick="shezhi(id=this.id)">
            <input value="取消" type="button" class=" btn-default" onclick="guanbi()">
        </div>
    </div>
    <div style="display:none;position:fixed;z-index: 210;width: 400px;top: 100px;left: 30%;box-shadow: 0 0 10px rgba(0,0,0,0.6);border: solid 1px #255c71;border-radius: 5px 5px 0 0;background-color: #9eacb4"
         id="adduser">
        <div style="height: 5px;">添加用户</div>
        <div style="background-color: whitesmoke;margin-top: 24px">
            <p>
                用户名：<input type="text" id="name" placeholder="请输入用户名">
            </p>
            <p>
                登录密码：<input type="password" id="password">
            </p>
            <p>
                确认登录密码：<input type="password" id="password1">
            </p>
            <p>
                email：<input type="text" id="email">
            </p>
            <p>
                职位：<select id="zhiwei">
                {% for zhiwei in wroks %}
                    <option>{{ zhiwei.name }}</option>
                {% endfor %}
            </select>
            </p>
            <p>
                选择用户所属项目：
                <select id="project" multiple>
                    {% for projet in projects %}
                        <option>{{ projet.project_name }}</option>
                    {% endfor %}
                </select>
            </p>
        </div>
        <div style="margin-top: 5%;margin-bottom: auto">
            <input value="添加" type="button" class="btn-default" onclick="add_user()">
            <input value="取消" type="button" class=" btn-default" onclick="guanbiadd()">
        </div>
    </div>
{% endblock %}
{% block js %}
    <script language="JavaScript">
        function shezhiguanliyuan(id) {
            var divshow = document.getElementById('ceshiyongli');
            var divproject = document.getElementsByClassName(id);
            if (divproject.length <= 0) {
                alert('没有所属项目 不能设置管理员')
            } else {
                var idshezhi = document.getElementsByClassName('shezhiguanliyuan');
                idshezhi[0].setAttribute('id', id);
                divshow.style.display = 'block';
                var xuanz = $('#xuanzehuanjing');
                xuanz.empty();
                for (var i = 0; i < divproject.length; i++) {
                    var url = divproject[i];
                    xuanz.append("<option >" + url.innerText + "</option>");
                }
            }
        }
        function shezhi(id) {
            var me = document.getElementById("xuanzehuanjing");
            var index = document.getElementById("xuanzehuanjing").selectedIndex;
            var ur = me.options[index];
            var data = {};
            if (ur == undefined) {
                ursl = '';
            }
            else {
                ursl = ur.text;
            }
            data['username'] = id;
            data['url'] = ursl;
            $.ajax({
                url: '{{ url_for('user.set_ad') }}',
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=UTF-8',
                processData: false,
                success: function (result) {
                    if (result['code'] == 200) {
                        window.location.reload();
                        alert(result['msg'])
                    }
                    else {
                        window.location.reload();
                        alert(result['msg'])
                    }
                }
            })

        }
        function guanbi() {
            var divshow = document.getElementById('ceshiyongli');
            divshow.style.display = 'none';
        }
        function adduser() {
            m = document.getElementById('adduser');
            m.style.display = ''
        }
        function guanbiadd() {
            m = document.getElementById('adduser');
            m.style.display = 'none'
        }
        function add_user() {
            var name = document.getElementById('name').value;
            var password = document.getElementById('password').value;
            var queren = document.getElementById('password1').value;
            var zhiwei = document.getElementById('zhiwei');
            var email=document.getElementById('email').value;
            var index = zhiwei.selectedIndex;
            var work = zhiwei.options[index].value;
            var proobj = document.getElementById("project");
            var selectedValues = [];
            for (var i = 0; i < proobj.length; i++) {
                if (proobj.options[i].selected == true) {
                    selectedValues.push(proobj[i].text);
                }
            }
            var data={};
            data['name']=name;
            data['password']=password;
            data['work']=work;
            data['project']=selectedValues;
            data['email']=email;
            if (password != queren){
                alert('请确认两个密码是否一致')
            }else {
              $.ajax({
                    url: '{{url_for( 'home.adminuser') }}',
                    type: 'POST',
                    data: JSON.stringify(data),
                    contentType: 'application/json; charset=UTF-8',
                    processData: false,
                    success: function (result) {
                        if(result['code']==2){
                            alert(result['data']);
                            location.reload()
                        }
                        else{
                            alert(result['data']);
                        }
                    }
                }
            )
            }
        }
    </script>
{% endblock %}